package com.zidian.study.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zidian.study.globa.ActManager;
import com.zidian.study.globa.BaseEntity;
import com.zidian.study.globa.Constant;
import com.zidian.study.globa.ToolBarActivity;
import com.zidian.study.utils.GetUserInfo;
import com.zidian.study.utils.ProgressBarBlackUtil;
import com.zidian.study.utils.ToastUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import com.zidian.student.trucks.R;

/**
 * Created by Administrator on 2016/8/28 0028.
 * 修改密码
 */
public class StudySetPasswordActivity extends ToolBarActivity implements TextWatcher {

    @Bind(R.id.study_change_old_psw)
    EditText studyChangeOldPsw;
    @Bind(R.id.study_change_new_psw)
    EditText studyChangeNewPsw;
    @Bind(R.id.study_change_sure_psw)
    EditText studyChangeSurePsw;
    @Bind(R.id.submit_btn)
    Button submitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_study_password);
        ButterKnife.bind(this);
        setTitle("修改密码");
        initView();
    }

    private void initView() {
        studyChangeNewPsw.addTextChangedListener(this);
        studyChangeSurePsw.addTextChangedListener(this);
        studyChangeOldPsw.addTextChangedListener(this);

    }

    @OnClick(R.id.submit_btn)
    public void submit() {
        submitBtn.setEnabled(false);
        if (studyChangeOldPsw.getText().toString().equals(studyChangeNewPsw.getText().toString())) {
            Snackbar.make(submitBtn, "新密码跟旧密码不能相同", Snackbar.LENGTH_SHORT).show();
            return;
        }
        if (studyChangeNewPsw.getText().toString().length() <6 ||
                studyChangeNewPsw.getText().toString().length() > 15) {
            Snackbar.make(submitBtn, "请输入合法长度的密码", Snackbar.LENGTH_SHORT).show();
            return;
        }
        if (studyChangeNewPsw.getText().toString().equals(studyChangeSurePsw.getText().toString())) {
            try {
                changePSW();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Snackbar.make(submitBtn,"两次输入的密码不一致!",Snackbar.LENGTH_SHORT).show();
        }
    }

    private void changePSW() throws IOException {
        String loginUrl = Constant.URL_GOAL + "studentApp/updPassword";
        HashMap map = new HashMap();
        map.put("studentId", GetUserInfo.getStudentId(context));
        map.put("password", studyChangeOldPsw.getText().toString().trim());
        map.put("password1", studyChangeNewPsw.getText().toString().trim());
        map.put("password2", studyChangeSurePsw.getText().toString().trim());
        map.put("studentId", GetUserInfo.getStudentId(context));
        map.put("token", GetUserInfo.getToken(context));
        ProgressBarBlackUtil.loadShow(context);
        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
                ProgressBarBlackUtil.loadCancel();
            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                BaseEntity base = gson.fromJson(response, BaseEntity.class);
                if (base.isSuccess()) {
                    ProgressBarBlackUtil.loadCancel();
                    ToastUtil.showToast(context, "修改成功");
                    ActManager.getAppManager().finishAllActivity();
                    GetUserInfo.loginOut(context);
                    startActivity(LoginActivity.class);
                } else {
                    ToastUtil.showToast(context, base.getMessage());
                    ProgressBarBlackUtil.loadCancel();
                }
            }
        });

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        boolean newpsw = studyChangeNewPsw.getText().toString().trim().length() > 0;
        boolean surepsw = studyChangeSurePsw.getText().toString().trim().length() > 0;
        boolean oldpsw = studyChangeOldPsw.getText().toString().trim().length() > 0;

        if (newpsw && oldpsw && surepsw) {
            submitBtn.setEnabled(true);
        } else {
            submitBtn.setEnabled(false);
        }


    }

    @Override
    public void afterTextChanged(Editable s) {

    }


}
